<template>
  <div class="pagination">
    <button @click="$emit('get-pageNo', pageNo - 1)" :disabled="pageNo === 1">
      上一页
    </button>
    <button v-show="startAndEnd.startPage > 1" @click="$emit('get-pageNo', 1)">
      1
    </button>
    <span v-show="startAndEnd.startPage > 2">...</span>
    <button
      @click="TTpageNo(index + startAndEnd.startPage)"
      :class="{ active: index + startAndEnd.startPage === pageNo }"
      v-for="(_, index) in startAndEnd.endPage - startAndEnd.startPage + 1"
      :key="_"
    >
      {{ index + startAndEnd.startPage }}
    </button>
    <button v-show="startAndEnd.endPage < totalPage - 1">···</button>
    <button
      v-show="startAndEnd.endPage < totalPage"
      @click="$emit('get-pageNo', totalPage)"
    >
      总页数:{{ totalPage }}
    </button>
    <button @click="$emit('get-pageNo', pageNo + 1)" :disabled="pageNo === 1">
      下一页
    </button>

    <span>共 {{ total }} 条</span>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  // props: ["total", "pageSize", "continues", "pageNo"],
  props: {
    total: {
      type: Number,
      default: 1,
    },
    pageNo: {
      type: Number,
      default: 1,
    },
    pageSize: {
      type: Number,
      default: 0,
    },
    continues: {
      type: Number,
      default: 3,
    },
  },
  computed: {
    // 计算总页码数
    totalPage() {
      let { total, pageSize } = this;
      // 向上取整
      return Math.ceil(total / pageSize);
    },
    startAndEnd() {
      let startPage, endPage;
      let { pageNo, continues, totalPage } = this;
      if (continues > totalPage) {
        startPage = 1;
        endPage = totalPage;
      } else {
        // 计算起始页
        startPage = pageNo - Math.trunc(continues / 2);
        // 计算结束页   // Math.trunc 这个是取整数的方法 只会取整数部分
        endPage = pageNo + Math.trunc(continues / 2);

        if (startPage < 1) {
          startPage = 1;
          endPage = totalPage;
        }
        if (endPage > totalPage) {
          endPage = totalPage;
          startPage = totalPage - continues + 1;
        }
      }
      return { startPage, endPage };
    },
  },
  methods: {
    // 事件将当前页面传给父组件
    TTpageNo(pageNo) {
      this.$emit("get-pageNo", pageNo);
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #c81623;
      color: #fff;
    }
  }
  span {
    display: inline-block;
    line-height: 28px;
    font-size: 14px;
    color: gray;
    vertical-align: middle;
  }
}
</style>